package com.hzit.mmcb.core.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hzit.common.exception.Assert;
import com.hzit.common.result.ResponseEnum;
import com.hzit.mmcb.core.enums.UserBindEnum;
import com.hzit.mmcb.core.hfb.FormHelper;
import com.hzit.mmcb.core.hfb.HfbConst;
import com.hzit.mmcb.core.hfb.RequestHelper;
import com.hzit.mmcb.core.pojo.entity.UserBind;
import com.hzit.mmcb.core.mapper.UserBindMapper;
import com.hzit.mmcb.core.pojo.entity.UserInfo;
import com.hzit.mmcb.core.pojo.vo.UserBindVO;
import com.hzit.mmcb.core.service.UserBindService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hzit.mmcb.core.service.UserInfoService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 用户绑定表 服务实现类
 * </p>
 *
 * @author ljh
 * @since 2024-08-27
 */
@Service
public class UserBindServiceImpl extends ServiceImpl<UserBindMapper, UserBind> implements UserBindService {

    @Autowired
    private UserInfoService userInfoService;
    @Override
    public String commitBindUser(UserBindVO userBindVO, Long userId) {
        QueryWrapper<UserBind> userBindQueryWrapper=new QueryWrapper<>();
        userBindQueryWrapper.eq("id_card",userBindVO.getIdCard())
                .ne("user_id",userId);
        UserBind one = baseMapper.selectOne(userBindQueryWrapper);
        Assert.isNull(one, ResponseEnum.USER_BIND_IDCARD_EXIST_ERROR);
        userBindQueryWrapper=new QueryWrapper<>();
        userBindQueryWrapper.eq("user_id",userId);
        UserBind userBind = baseMapper.selectOne(userBindQueryWrapper);
        if (userBind==null){
            userBind=new UserBind();
            BeanUtils.copyProperties(userBindVO,userBind);
            userBind.setStatus(UserBindEnum.NO_BIND.getStatus());
            userBind.setUserId(userId);
            baseMapper.insert(userBind);
        }else {
            BeanUtils.copyProperties(userBindVO,userBind);
            baseMapper.updateById(userBind);
        }
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("agentId", HfbConst.AGENT_ID);
        paramMap.put("agentUserId", userId);
        paramMap.put("idCard",userBindVO.getIdCard());
        paramMap.put("personalName", userBindVO.getName());
        paramMap.put("bankType", userBindVO.getBankType());
        paramMap.put("bankNo", userBindVO.getBankNo());
        paramMap.put("mobile", userBindVO.getMobile());
        paramMap.put("returnUrl", HfbConst.USERBIND_RETURN_URL);
        paramMap.put("notifyUrl", HfbConst.USERBIND_NOTIFY_URL);
        paramMap.put("timestamp", RequestHelper.getTimestamp());
        paramMap.put("sign", RequestHelper.getSign(paramMap));
        String formStr = FormHelper.buildForm(HfbConst.USERBIND_URL,paramMap);
        return formStr;
    }

    /**
     * 汇付宝发出的异步回调
     * @param map
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void mmcbNotify(Map<String, Object> map) {
        String userIdStr=map.get("agentUserId")+"";
        if (StrUtil.isNotBlank(userIdStr)){
            String bindCode=map.get("bindCode")+"";
            Long userId=Long.parseLong(userIdStr);
            //获得绑定用户信息
            UserBind userBind = this.getOne(new QueryWrapper<UserBind>().eq("user_id", userId));
            //修改绑定信息
            userBind.setStatus(UserBindEnum.BIND_OK.getStatus());
            userBind.setBindCode(bindCode);
            this.updateById(userBind);

            //绑定userInfo表
            UserInfo userInfo = userInfoService.getById(userId);
            userInfo.setName(userBind.getName());
            userInfo.setIdCard(userBind.getIdCard());
            userInfo.setBindStatus(userBind.getStatus());
            userInfo.setBindCode(bindCode);
            userInfoService.updateById(userInfo);
        }
    }
}
